<?php
/**
 * Class that operate on table 'hs_school'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2012-05-30 11:06
 */
class HsSchoolMySqlDAO implements HsSchoolDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return HsSchoolMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM hs_school WHERE schoolID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM hs_school';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM hs_school ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param hsSchool primary key
 	 */
	public function delete($schoolID){
		$sql = 'DELETE FROM hs_school WHERE schoolID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($schoolID);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param HsSchoolMySql hsSchool
 	 */
	public function insert($hsSchool){
		$sql = 'INSERT INTO hs_school (schoolName, schoolStAddress1, schoolStAddress2, schoolCity, schoolState, schoolCountry, schoolZipCode) VALUES (?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsSchool->schoolName);
		$sqlQuery->set($hsSchool->schoolStAddress1);
		$sqlQuery->set($hsSchool->schoolStAddress2);
		$sqlQuery->set($hsSchool->schoolCity);
		$sqlQuery->set($hsSchool->schoolState);
		$sqlQuery->set($hsSchool->schoolCountry);
		$sqlQuery->set($hsSchool->schoolZipCode);

		$id = $this->executeInsert($sqlQuery);	
		$hsSchool->schoolID = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param HsSchoolMySql hsSchool
 	 */
	public function update($hsSchool){
		$sql = 'UPDATE hs_school SET schoolName = ?, schoolStAddress1 = ?, schoolStAddress2 = ?, schoolCity = ?, schoolState = ?, schoolCountry = ?, schoolZipCode = ? WHERE schoolID = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsSchool->schoolName);
		$sqlQuery->set($hsSchool->schoolStAddress1);
		$sqlQuery->set($hsSchool->schoolStAddress2);
		$sqlQuery->set($hsSchool->schoolCity);
		$sqlQuery->set($hsSchool->schoolState);
		$sqlQuery->set($hsSchool->schoolCountry);
		$sqlQuery->set($hsSchool->schoolZipCode);

		$sqlQuery->set($hsSchool->schoolID);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM hs_school';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryBySchoolName($value){
		$sql = 'SELECT * FROM hs_school WHERE schoolName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySchoolStAddress1($value){
		$sql = 'SELECT * FROM hs_school WHERE schoolStAddress1 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySchoolStAddress2($value){
		$sql = 'SELECT * FROM hs_school WHERE schoolStAddress2 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySchoolCity($value){
		$sql = 'SELECT * FROM hs_school WHERE schoolCity = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySchoolState($value){
		$sql = 'SELECT * FROM hs_school WHERE schoolState = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySchoolCountry($value){
		$sql = 'SELECT * FROM hs_school WHERE schoolCountry = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySchoolZipCode($value){
		$sql = 'SELECT * FROM hs_school WHERE schoolZipCode = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteBySchoolName($value){
		$sql = 'DELETE FROM hs_school WHERE schoolName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySchoolStAddress1($value){
		$sql = 'DELETE FROM hs_school WHERE schoolStAddress1 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySchoolStAddress2($value){
		$sql = 'DELETE FROM hs_school WHERE schoolStAddress2 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySchoolCity($value){
		$sql = 'DELETE FROM hs_school WHERE schoolCity = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySchoolState($value){
		$sql = 'DELETE FROM hs_school WHERE schoolState = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySchoolCountry($value){
		$sql = 'DELETE FROM hs_school WHERE schoolCountry = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySchoolZipCode($value){
		$sql = 'DELETE FROM hs_school WHERE schoolZipCode = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return HsSchoolMySql 
	 */
	protected function readRow($row){
		$hsSchool = new HsSchool();
		
		$hsSchool->schoolName = $row['schoolName'];
		$hsSchool->schoolStAddress1 = $row['schoolStAddress1'];
		$hsSchool->schoolStAddress2 = $row['schoolStAddress2'];
		$hsSchool->schoolCity = $row['schoolCity'];
		$hsSchool->schoolState = $row['schoolState'];
		$hsSchool->schoolCountry = $row['schoolCountry'];
		$hsSchool->schoolZipCode = $row['schoolZipCode'];
		$hsSchool->schoolID = $row['schoolID'];

		return $hsSchool;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return HsSchoolMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>